<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>MIFARE ONE DEMO</title>
    <style type="text/css">

        select {
            width: 150px; /* 设置下拉框的宽度 */
            padding: 5px; /* 设置内边距 */
            margin: 10px; /* 设置外边距 */
            font-size: 18px; /* 设置字体大小 */
            border: 1px solid #ccc; /* 设置边框 */
            border-radius: 5px; /* 设置边框圆角 */
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); /* 设置阴影效果 */
            cursor: pointer; /* 鼠标悬停时显示指针 */
        }

        select:focus {
            border-color: #007bff; /* 当下拉框获得焦点时改变边框颜色 */
            outline: none; /* 移除默认的轮廓 */
        }

        /* 假设这个CSS被包含在一个<style>标签内，或者在一个外部的.css文件中 */
        #btnConnect {
            /* 设置按钮的背景颜色 */
            background-color: #549BFD;
            /* 设置边框，这里设置为圆角边框，并且去掉默认边框 */
            border: none;
            border-radius: 5px;
            /* 设置按钮内的文字颜色 */
            color: white;
            /* 设置字体样式和大小 */
            font-size: 16px;
            font-weight: bold;
            /* 设置按钮的宽度和高度 */
            width: 110px;
            height: 40px;
            /* 添加鼠标悬停效果 */
            transition: background-color 0.3s; /* 平滑过渡效果 */
        }

        #btnConnect:hover {
            /* 鼠标悬停时改变背景颜色 */
            background-color: #0056b3;
        }

        #btnReadBlock {
            /* 设置按钮的背景颜色 */
            background-color: #549BFD;
            /* 设置边框，这里设置为圆角边框，并且去掉默认边框 */
            border: none;
            border-radius: 5px;
            /* 设置按钮内的文字颜色 */
            color: white;
            /* 设置字体样式和大小 */
            font-size: 16px;
            font-weight: bold;
            /* 设置按钮的宽度和高度 */
            width: 110px;
            height: 40px;
            /* 添加鼠标悬停效果 */
            transition: background-color 0.3s; /* 平滑过渡效果 */
        }

        #btnReadBlock:hover {
            /* 鼠标悬停时改变背景颜色 */
            background-color: #0056b3;
        }


        #btnWriteBlock {
            /* 设置按钮的背景颜色 */
            background-color: #549BFD;
            /* 设置边框，这里设置为圆角边框，并且去掉默认边框 */
            border: none;
            border-radius: 5px;
            /* 设置按钮内的文字颜色 */
            color: white;
            /* 设置字体样式和大小 */
            font-size: 16px;
            font-weight: bold;
            /* 设置按钮的宽度和高度 */
            width: 110px;
            height: 40px;
            /* 添加鼠标悬停效果 */
            transition: background-color 0.3s; /* 平滑过渡效果 */
        }

        #btnWriteBlock:hover {
            /* 鼠标悬停时改变背景颜色 */
            background-color: #0056b3;
        }

        #tfTips {
            /* 设置边框样式 */
            border: 2px solid #007bff; /* 使用蓝色边框，你可以根据需要更改颜色 */
            /* 设置边框圆角 */
            border-radius: 5px; /* 圆角大小可以根据喜好调整 */
            /* 设置内边距，让文字不会紧贴边框 */
            padding: 8px;
            /* 设置输入框的字体样式 */
            font-size: 16px; /* 字体大小 */
            font-family: Arial, sans-serif; /* 字体类型，这里使用了Arial，你也可以使用其他字体 */
            /* 设置输入框的宽度，这里使用了100%使其宽度自适应容器，你也可以设置为固定宽度 */
            width: 100%; /* 或者使用具体像素值，如300px */
            /* 移除默认的外边距（如果需要的话） */
            margin: 0;
            /* 设置输入框的焦点样式（可选） */
            //box-shadow: 0 0 5px rgba(0, 123, 255, 0.5) inset; /* 聚焦时添加内阴影效果 */
            ///* 过渡效果（可选），使边框颜色变化更加平滑 */
            //transition: border-color 0.3s, box-shadow 0.3s;
        }

        #tfBlockData {
            /* 设置边框样式 */
            border: 2px solid #007bff; /* 使用蓝色边框，你可以根据需要更改颜色 */
            /* 设置边框圆角 */
            border-radius: 5px; /* 圆角大小可以根据喜好调整 */
            /* 设置内边距，让文字不会紧贴边框 */
            padding: 8px;
            /* 设置输入框的字体样式 */
            font-size: 16px; /* 字体大小 */
            font-family: Arial, sans-serif; /* 字体类型，这里使用了Arial，你也可以使用其他字体 */
            /* 设置输入框的宽度，这里使用了100%使其宽度自适应容器，你也可以设置为固定宽度 */
            width: 100%; /* 或者使用具体像素值，如300px */
            /* 移除默认的外边距（如果需要的话） */
            margin: 0;
            /* 设置输入框的焦点样式（可选） */
            //box-shadow: 0 0 5px rgba(0, 123, 255, 0.5) inset; /* 聚焦时添加内阴影效果 */
            /* 过渡效果（可选），使边框颜色变化更加平滑 */
            //transition: border-color 0.3s, box-shadow 0.3s;
        }

        .info-table {
            width: 100%; /* 表格宽度 */
            border-collapse: collapse; /* 边框合并 */
            margin-top: 20px; /* 顶部外边距 */
        }

        .info-table td {
            border: 1px solid #ddd; /* 单元格边框 */
            padding: 8px; /* 内边距 */
            text-align: left; /* 文本左对齐 */
            background-color: #f9f9f9; /* 背景色 */
        }

        .label-text {
            font-size: 18px; /* 字体大小 */
            font-weight: bold; /* 字体加粗 */
            margin-right: 10px; /* 右侧外边距，与值分隔 */
        }

        .label-value {
            font-size: 18px; /* 字体大小 */
        }

    </style>
</head>

<script type="text/javascript" src="rdapi.js"></script>


<body>
<div style="display: flex;flex-direction: column;align-items: center;justify-content: center">
    <table width="550">
        <tr>
            <th height="24" colspan="6" scope="col">
                <form id="form1" name="form1" method="post" action="">
                    <div align="left">
                    </div>
                </form>
            </th>
        </tr>
        <tr>
            <td height="19" colspan="6">&nbsp;</td>
        </tr>


        <tr>
            <td height="19" colspan="6">
                <label for="userLevel" class="label-text">用户名称：</label>
                <label id="username" class="label-value">用户名称</label>
            </td>
        </tr>

        <tr>
            <td height="19" colspan="6">
                <label for="userLevel" class="label-text">机器名称：</label>
                <label id="mname" class="label-value">机器名称</label>

            </td>
        </tr>

        <tr>
            <td height="19" colspan="6">
                <label for="userLevel" class="label-text">开始时间：</label>
                <label id="start" class="label-value">开始时间</label>
            </td>
        </tr>

        <tr>
            <td height="19" colspan="6">
                <label for="userLevel" class="label-text">结束时间：</label>
                <label id="end" class="label-value">结束时间</label>
            </td>
        </tr>

        <tr>
            <td height="19" colspan="6">
                <label for="userLevel" class="label-text">设备编号：</label>
                <label id="mid" class="label-value">设备编号</label>
            </td>
        </tr>

        <tr>
            <td style="height: 20px"></td>
        </tr>

        <!--        <tr>-->
        <!--            <td height="19" colspan="6">-->
        <!--                <label for="userLevel" style="font-size: 18px">设备编号：</label>-->
        <!--                <input name="tfBlockData" type="text" id="mid" size="40" maxlength="32" readonly/>-->
        <!--            </td>-->
        <!--        </tr>-->


        <!--        <tr>-->
        <!--            <td height="30">-->
        <!--                <div align="left" style="font-size: 18px">设备编号 :</div>-->
        <!--            </td>-->
        <!--            <td>&nbsp;</td>-->
        <!--            <td colspan="4">-->
        <!--                <form id="form2" name="form2" method="post" action="">-->
        <!--                    <label for="tfBlockData"></label>-->
        <!--                    <input name="tfBlockData" type="text" id="mid" size="40" maxlength="32" readonly/>-->
        <!--                </form>-->
        <!--            </td>-->
        <!--        </tr>-->


        <tr>
            <td height="30">
                <div align="left" style="font-size: 18px">写入数据 :</div>
            </td>
            <td>&nbsp;</td>
            <td colspan="4">
                <form id="form2" name="form2" method="post" action="">
                    <label for="tfBlockData"></label>
                    <input name="tfBlockData" type="text" id="tfBlockData" size="40" maxlength="32" readonly/>
                </form>
            </td>
        </tr>
        <tr>
            <td style="height: 20px"></td>
        </tr>
        <tr>
            <td height="30">&nbsp;</td>
            <td height="31">&nbsp;</td>
            <td height="31" colspan="2">
                <div align="center">
                    <!--                    <input  name="btnReadBlock" type="button" id="btnReadBlock" onclick="ReadBlock()" value=" 读取 "/>-->
                    <input name="btnConnect" type="button" id="btnConnect" onclick="Connect()" value="连接读卡器"/>
                </div>
            </td>
            <td width="98"><input name="btnWriteBlock" type="button" id="btnWriteBlock" onclick="WriteBlock()"
                                  value="写入"/></td>
            <td width="174">&nbsp;</td>
        </tr>
        <tr>
            <td style="height: 50px"></td>
        </tr>

        <tr>
            <td height="30" colspan="6" style="color:#549BFD;font-size: 18px"><strong> 执行反馈</strong>
                <form style="margin-top: 5px" id="form10" name="form10" method="post" action="">
                    <label for="tfTips"></label>
                    <input name="tfTips" type="text" id="tfTips" size="70" maxlength="70" readonly/>
                </form>
            </td>
        </tr>

    </table>
</div>

<script type="text/javascript">

    // 当页面加载完成后，读取 URL 中的查询参数
    window.onload = function () {
        const urlParams = new URLSearchParams(window.location.search);
        const message = JSON.parse(urlParams.get('message'));
        // 将获取的信息显示在页面上'
        console.log('message', message)
        //拿到的数据
        //"bId": 29,
        //"mname": "测试1",
        //"mcode": "0001",
        //"start": "2024-07-01T00:00:00.000Z", 开始时间
        // "end": "2024-07-02T04:00:00.000Z",  结束时间
        //"uname": "张三",
        //"eStatus": "0",
        //"bStatus": 1,
        //"memo": ""

        console.log(message.start)
        console.log(message.end)


        startString = formatChineseDateTime(message.start)
        endString = formatChineseDateTime(message.end)


        document.getElementById('start').textContent = startString;
        document.getElementById('end').textContent = endString;
        document.getElementById('username').textContent = message.uname;
        document.getElementById('mname').textContent = message.mname;
        document.getElementById('mid').textContent = message.mId;


        one = formatDate(message.start)
        two = formatDate(message.end)
        mmID = padToThreeDigits(message.mId)
        final = mmID + one + two + '000000000'

        document.getElementById("tfBlockData").value = final

        console.log(startString)
        console.log(endString)
        console.log(one)
        console.log(two)
        console.log(final)

        // 使用示例：
        // const bId = 118; // 你的设备id
        // const paddedBId = padToThreeDigits(bId); // 转换为三位数字符串
        // console.log(paddedBId); // 输出: 001
        // console.log(111111111111111111)
    };


    var GFUNC = {
        M1_findCard: 1,
        M1_authentication: 2,
        M1_read: 3,
        M1_write: 4,
        M1_initVal: 5,
        M1_increment: 6,
        M1_decrement: 7,
        M1_readVal: 8,
        M1_updateKey: 9,
    };

    var g_device = "00"; //设备句柄号
    var g_isOpen = false;
    var g_blockAddr;
    var g_blockData;
    var g_key;
    var g_keyType = "60";
    var g_vale;
    var g_wantFunc = 0;
    var startString = ''
    var endString = ''
    var one = ''
    var two = ''
    var final = ''
    var bbID = ''


    /**
     * Reader callback function，the command response is processed here
     * (读卡器回调函数，在这里对命令应答进行处理)
     **/
    reader.onResult(function (rData) {
            // console.log('rData', rData)
            // console.log('rData.type', rData.type)
            // console.log('rData.strStatus', rData.strStatus)
            // console.log('rData.strData', rData.strData)
            // console.log('rData.strCmdCode', rData.strCmdCode)
            switch (rData.strCmdCode) {
                case "0007": //Sys_Open
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "设备连接失败 ! " + "错误码: 0x" + rData.strStatus;
                    } else {
                        g_isOpen = true;
                        document.getElementById('tfTips').value = "读卡器连接成功 !";
                    }
                    break;

                case "0009": //Sys_Close
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "Failed to disconnect device !";
                    } else {
                        document.getElementById('tfTips').value = "Reader disconnected successfully !";
                    }
                    break;

                case "0106": //Sys_SetBuzzer
                    break;

                case "0105": //Sys_GetSnr
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "Sys_GetSnr faild !";
                    } else {
                        document.getElementById('tfTips').value = rData.strData;
                        document.getElementById('tfTips').value = "Get device serial number successfully !";
                    }
                    break;

                case "1001": //TyA_Request
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "未读到卡 !";
                        return;
                    }

                    switch (g_wantFunc) {
                        case GFUNC.M1_findCard:
                        case GFUNC.M1_authentication:
                        case GFUNC.M1_read:
                        case GFUNC.M1_write:
                        case GFUNC.M1_initVal:
                        case GFUNC.M1_increment:
                        case GFUNC.M1_decrement:
                        case GFUNC.M1_readVal:
                        case GFUNC.M1_updateKey:
                            reader.send(g_device + "1002"); //TyA_Anticollision
                            break;
                    }

                    break;

                case "1002": //TyA_Anticollision
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "TyA_Anticollision faild !";
                        return;
                    }

                    switch (g_wantFunc) {
                        case GFUNC.M1_findCard:
                            document.getElementById('tfUID').value = rData.strData;
                            document.getElementById('tfTips').value = "检测到卡！UID已填入uid的表单";
                        case GFUNC.M1_authentication:
                        case GFUNC.M1_read:
                        case GFUNC.M1_write:
                        case GFUNC.M1_initVal:
                        case GFUNC.M1_increment:
                        case GFUNC.M1_decrement:
                        case GFUNC.M1_readVal:
                        case GFUNC.M1_updateKey:
                            reader.send(g_device + "1003" + rData.strData); //TyA_Select
                            break;
                    }

                    break;

                case "1003": //TyA_Select
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "TyA_Select faild !";
                        return;
                    }

                    switch (g_wantFunc) {
                        case GFUNC.M1_authentication:
                        case GFUNC.M1_read:
                        case GFUNC.M1_write:
                        case GFUNC.M1_initVal:
                        case GFUNC.M1_increment:
                        case GFUNC.M1_decrement:
                        case GFUNC.M1_readVal:
                        case GFUNC.M1_updateKey:
                            reader.send(g_device + "100A" + g_keyType + g_blockAddr + g_key); //TyA_CS_Authentication2
                            break;
                    }

                    break;

                case "100A": //TyA_CS_Authentication2
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "TyA_CS_Authentication2 faild !";
                        return;
                    }

                    switch (g_wantFunc) {
                        case GFUNC.M1_read:
                            reader.send(g_device + "100B" + g_blockAddr); //TyA_CS_Read
                            break;

                        case GFUNC.M1_write:
                            reader.send(g_device + "100C" + g_blockAddr + g_blockData);
                            break;

                        case GFUNC.M1_initVal:
                            reader.send(g_device + "100D" + g_blockAddr + g_value);
                            break;

                        case GFUNC.M1_readVal:
                            reader.send(g_device + "100E" + g_blockAddr);
                            break;

                        case GFUNC.M1_decrement:
                            reader.send(g_device + "100F" + g_blockAddr + g_value);
                            break;

                        case GFUNC.M1_increment:
                            reader.send(g_device + "1010" + g_blockAddr + g_value);
                            break;
                    }

                    break;

                case "100B": //TyA_CS_Read
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "读取失败 !";
                    } else {
                        document.getElementById('tfBlockData').value = rData.strData;
                        document.getElementById('tfTips').value = "读取信息成功 !";
                    }
                    break;

                case "100C": //TyA_CS_Write
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "TyA_CS_Write faild !";
                    } else {
                        document.getElementById('tfTips').value = "写入信息成功 !";
                    }
                    break;

                case "100D": //TyA_CS_InitValue
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "TyA_CS_InitValue faild !";
                    } else {
                        document.getElementById('tfTips').value = "Initialize the wallet value successfully !";
                    }
                    break;

                case "100E": //TyA_CS_ReadValue
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "TyA_CS_ReadValue faild !";
                    } else {
                        var hexValue = rData.strData;
                        hexValue = hexValue.substr(6, 2) + hexValue.substr(4, 2) + hexValue.substr(2, 2) + hexValue.substr(0, 2); //Reverse sorting of high and low bytes (高低字节反过来排序)
                        var decValue = parseInt(hexValue, 16); //Convert hexadecimal string to decimal string (十六进制字符串转换为十进制字符串)
                        document.getElementById("tfValue").value = decValue; //Show wallet balance (显示电子钱包余额)
                        document.getElementById('tfTips').value = "Read value successfully !";
                    }
                    break;

                case "100F": //TyA_CS_Decrement
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "TyA_CS_Decrement faild !";
                    } else {
                        document.getElementById('tfTips').value = "Decrement value successfully !";
                    }
                    break;

                case "1010": //TyA_CS_Increment
                    if (rData.strStatus !== "00") {
                        document.getElementById('tfTips').value = "TyA_CS_Increment faild !";
                    } else {
                        document.getElementById('tfTips').value = "Increment value successfully !";
                    }
                    break;
            }

        }
    );

    //不足三位补足三位
    function padToThreeDigits(number) {
        return String(number).padStart(3, '0');
    }

    //时间转换方法
    function convertToTimestamp(isoString) {
        return new Date(isoString).getTime();
    }


    /**
     * Function：Converts a decimal string to a hexadecimal string with a specified number of digits.
     (十进制字符串转换为指定位数的十六进制字符串)
     * Parameter：decimalStr [IN] Decimal string. (十进制字符串)
     *            length [IN] Specifies the number of digits to convert to hexadecimal. (指定要转换成十六进制的位数)
     * Return：Hexadecimal string. (十六进制字符串)
     **/
    function DecStrToHexStr(decimalStr, length) {
        var num = Number(decimalStr);
        var str = (Array(length).join('0') + num.toString(16)).slice(-length);
        ;
        return str;
    }


    /**
     * Turn on the green light
     * (亮绿灯)
     **/
    function LedGreen() {
        reader.send(g_device + "0107" + "02");
    }

    /**
     * Turn on the red light
     * (亮红灯)
     **/
    function LedRed() {
        reader.send(g_device + "0107" + "01");
    }

    /**
     * Connect the reader and initialize the working mode
     * (连接读卡器并初始化工作模式)
     **/
    function Connect() {
        reader.send(g_device + "0007" + "00"); //Open the USB device with index number 0. (打开索引号为0的USB设备)
        reader.send(g_device + "0109" + "41"); //Set to ISO14443a working mode. (设置为ISO14443A工作模式)
        reader.send(g_device + "0108" + "01"); //Turn on the reader antenna. (打开读卡器天线)
        LedGreen();
        setTimeout("LedRed()", "200");
        // reader.send(g_device + "0106" + "10"); //Beeps. (蜂鸣提示)
    }

    /**
     * Reading card number
     * (寻卡并读卡号)
     **/
    function Request() {
        //Check whether the reader is opened or not.
        if (g_isOpen !== true) {
            document.getElementById("tfTips").value = "请先点击 【连接读卡器】，再进行后续操作 !";
            return;
        }

        //Clear UID edit box
        document.getElementById('tfUID').value = "";

        //Start read UID
        reader.send(g_device + "1001" + "52"); //TyA_Request
        g_wantFunc = GFUNC.M1_findCard;
    }

    /**
     * Read a block of M1 card
     * (读M1卡的一个块)
     **/
    function ReadBlock() {
        //Check whether the reader is opened or not.
        if (g_isOpen !== true) {
            document.getElementById("tfTips").value = "请先连接读卡器设备 !";
            return;
        }

        //Clear block data
        document.getElementById('tfBlockData').value = "";

        //Get block address
        g_blockAddr = '1';
        if (g_blockAddr === "") {
            document.getElementById("tfTips").value = "请输入block地址 !";
            return;
        }
        g_blockAddr = DecStrToHexStr(g_blockAddr, 2);

        //Get key
        g_key = 'FFFFFFFFFFFF';
        if (g_key.length !== 12) {
            document.getElementById("tfTips").value = "请输入一个12位的Key !";
            return;
        }


        //Start read block
        reader.send(g_device + "1001" + "52"); //TyA_Request
        g_wantFunc = GFUNC.M1_read;
    }


    /**
     * Write a block of M1 card
     * (写M1卡的一个块)
     **/
    function WriteBlock() {
        //Check whether the reader is opened or not.
        if (g_isOpen !== true) {
            document.getElementById("tfTips").value = "请先连接设备 !";
            return;
        }

        //Get block address
        g_blockAddr = '1';
        if (g_blockAddr === "") {
            document.getElementById("tfTips").value = "请输入Block的地址 !";
            return;
        }
        g_blockAddr = DecStrToHexStr(g_blockAddr, 2);

        //Get key
        g_key = 'FFFFFFFFFFFF';
        if (g_key.length !== 12) {
            document.getElementById("tfTips").value = "请输入一个12位的key !";
            return;
        }


        //Get block data
        g_blockData = document.getElementById("tfBlockData").value;
        if (g_blockData.length !== 32) {
            document.getElementById("tfTips").value = "请输入一个32位长度的数据！";
            return;
        }

        //Start write block
        reader.send(g_device + "1001" + "52"); //TyA_Request
        g_wantFunc = GFUNC.M1_write;
    }


    /**
     * Initialize one block of M1 card as e-wallet, be careful not to use password blocks
     * (将M1卡的一个块初始化为电子钱包,注意不要使用密码块)
     **/
    function InitValue() {
        //Check whether the reader is opened or not.
        if (g_isOpen !== true) {
            document.getElementById("tfTips").value = "请先连接设备 !";
            return;
        }

        //Get block address
        g_blockAddr = '1';
        if (g_blockAddr === "") {
            document.getElementById("tfTips").value = "请输入Block的地址 !";
            return;
        }
        g_blockAddr = DecStrToHexStr(g_blockAddr, 2);

        //Get key
        g_key = 'FFFFFFFFFFFF';
        if (g_key.length !== 12) {
            document.getElementById("tfTips").value = "请输入一个12位的key !";
            return;
        }


        //Get value
        var decValue = document.getElementById("tfValue").value;
        if (decValue === "") {
            document.getElementById("tfTips").value = "请输入一个数据 !";
            return;
        }
        var hexValue = DecStrToHexStr(decValue, 8); //Converts a decimal string to an 8-bit hexadecimal string (十进制字符串转换为4字节即8位十六进制字符串)
        g_value = hexValue.substr(6, 2) + hexValue.substr(4, 2) + hexValue.substr(2, 2) + hexValue.substr(0, 2); //Reverse sorting of high and low bytes (高低字节反过来排序)

        //Start init value
        reader.send(g_device + "1001" + "52"); //TyA_Request
        g_wantFunc = GFUNC.M1_initVal;
    }

    /**
     * E-wallet recharge
     * (电子钱包充值)
     **/
    function IncValue() {
        //Check whether the reader is opened or not.
        if (g_isOpen !== true) {
            document.getElementById("tfTips").value = "请先连接设备 !";
            return;
        }

        //Get block address
        g_blockAddr = '1';
        if (g_blockAddr === "") {
            document.getElementById("tfTips").value = "请输入Block的地址 !";
            return;
        }
        g_blockAddr = DecStrToHexStr(g_blockAddr, 2);

        //Get key
        g_key = 'FFFFFFFFFFFF';
        if (g_key.length !== 12) {
            document.getElementById("tfTips").value = "请输入一个12位的key !";
            return;
        }


        //Get value
        var decValue = document.getElementById("tfValue").value;
        if (decValue === "") {
            document.getElementById("tfTips").value = "请输入一个数据 !";
            return;
        }
        var hexValue = DecStrToHexStr(decValue, 8); //Converts a decimal string to an 8-bit hexadecimal string (十进制字符串转换为4字节即8位十六进制字符串)
        g_value = hexValue.substr(6, 2) + hexValue.substr(4, 2) + hexValue.substr(2, 2) + hexValue.substr(0, 2); //Reverse sorting of high and low bytes (高低字节反过来排序)

        //Start init value
        reader.send(g_device + "1001" + "52"); //TyA_Request
        g_wantFunc = GFUNC.M1_increment;
    }

    /**
     * Electronic wallet deduction
     * (电子钱包扣款)
     **/
    function DecValue() {
        //Check whether the reader is opened or not.
        if (g_isOpen !== true) {
            document.getElementById("tfTips").value = "请先连接设备 !";
            return;
        }

        //Get block address
        g_blockAddr = '1';
        if (g_blockAddr === "") {
            document.getElementById("tfTips").value = "请输入Block的地址 !";
            return;
        }
        g_blockAddr = DecStrToHexStr(g_blockAddr, 2);

        //Get key
        g_key = 'FFFFFFFFFFFF';
        if (g_key.length !== 12) {
            document.getElementById("tfTips").value = "请输入一个12位的key !";
            return;
        }


        //Get value
        var decValue = document.getElementById("tfValue").value;
        if (decValue == "") {
            document.getElementById("tfTips").value = "请输入一个数据 !";
            return;
        }
        var hexValue = DecStrToHexStr(decValue, 8); //Converts a decimal string to an 8-bit hexadecimal string (十进制字符串转换为4字节即8位十六进制字符串)
        g_value = hexValue.substr(6, 2) + hexValue.substr(4, 2) + hexValue.substr(2, 2) + hexValue.substr(0, 2); //Reverse sorting of high and low bytes (高低字节反过来排序)

        //Start init value
        reader.send(g_device + "1001" + "52"); //TyA_Request
        g_wantFunc = GFUNC.M1_decrement;
    }

    /**
     * Read e-wallet balance
     * (读电子钱包余额)
     **/
    function ReadValue() {
        //Check whether the reader is opened or not.
        if (g_isOpen !== true) {
            document.getElementById("tfTips").value = "请先连接设备 !";
            return;
        }

        //Clear value edit box
        document.getElementById('tfValue').value = "";

        //Get block address
        g_blockAddr = '1';
        if (g_blockAddr === "") {
            document.getElementById("tfTips").value = "请输入Block的地址 !";
            return;
        }
        g_blockAddr = DecStrToHexStr(g_blockAddr, 2);

        //Get key
        g_key = 'FFFFFFFFFFFF';
        if (g_key.length !== 12) {
            document.getElementById("tfTips").value = "请输入一个12位的key !";
            return;
        }


        //Start init value
        reader.send(g_device + "1001" + "52"); //TyA_Request
        g_wantFunc = GFUNC.M1_readVal;
    }


    function updateBlockInput(value) {
        var tfBlock = document.getElementById('tfBlock');
        tfBlock.value = value;
        if (value === '1') {
            document.getElementById('tfBlockData').value = startString
        } else {
            document.getElementById('tfBlockData').value = endString
        }
    }

    document.getElementById('userLevel').addEventListener('change', function () {
        console.log('用户选择的等级是：', this.value);
        //根据选择的等级从新生成数字串

        // 首先，通过ID获取元素
        var element = document.getElementById("tfBlockData");
        element.value = this.value + element.value.slice(1);
        console.log(element.value)
    });


    function formatDateTime(dateTimeString) {
        // 将字符串转换为Date对象
        const date = new Date(dateTimeString);

        // 提取年、月、日、小时
        // 注意月份是从0开始的，所以要加1，并且我们不需要补0（除了年份已经是完整的）
        const year = date.getFullYear() + '年';
        const month = (date.getMonth() + 1) + '月';
        const day = date.getDate() + '日';
        // 小时转换为12小时制并添加'时'（如果需要24小时制，则直接使用getHours()）
        // 但这里看起来是24小时制，所以我们只添加'时'
        const hour = date.getHours() + '时';

        // 拼接成目标格式
        const formattedDateTime = year + month + day + hour;

        // 返回格式化后的日期时间字符串
        return formattedDateTime;
    }


    function formatChineseDateTime(isoString) {
        // 解析ISO字符串为Date对象
        const date = new Date(isoString);

        // 构建年月日字符串
        const year = date.getFullYear();
        const month = date.getMonth() + 1; // 月份从0开始，所以加1
        const day = date.getDate();

        // 构建时分秒字符串
        const hours = date.getHours();
        const minutes = date.getMinutes();
        const seconds = date.getSeconds();

        // 返回格式化的日期时间字符串
        return `${year}年${month}月${day}日 ${hours}时`;
    }

    function formatDate(dateStr) {
        // 创建一个Date对象
        const date = new Date(dateStr);

        // 获取年月日时分秒
        const year = String(date.getFullYear()).padStart(4, '0');
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');

        // 组合年月日时分秒
        const formattedDate = `${year}${month}${day}${hours}`;

        return formattedDate;
    }


</script>
</body>

</html>
